package com.eastmelon.blog.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eastmelon.basic.model.Pager;
import com.eastmelon.basic.model.SystemContext;
import com.eastmelon.blog.entity.Link;
import com.eastmelon.blog.service.IndexService;
import com.eastmelon.blog.service.LinkService;

@Controller("linkController")
@RequestMapping("/back/link")
public class LinkController {
	@Resource(name = "linkService")
	private LinkService linkService;
	@Resource(name = "indexService")
	private IndexService indexService;


	@RequestMapping("/linkInfor")
	public String linkInfor(Model model) {
		SystemContext.setPageSize(10);
		Pager<Link> pl = linkService.findPage();
		model.addAttribute("pl", pl);
		return "back/link/linkInfor";
	}

	@RequestMapping("/addLink")
	public String addLink(Link link) {
		Integer pos = linkService.getMaxPos();
		if(pos!=null)
		{
			link.setPos(pos+1);
		}else{
			link.setPos(1);
		}
		// 设置位置
		linkService.save(link);
		indexService.createNav();
		return "redirect:/back/link/linkInfor.do";
	}

	@RequestMapping("/updateLink")
	public String updateLink(Link link) {
		linkService.update(link);
		indexService.createNav();
		return "redirect:/back/link/linkInfor.do";
	}

	@RequestMapping("/deleteLink")
	public String delLink(int id) {
		linkService.deleteById(id);
		indexService.createNav();
		return "redirect:/back/link/linkInfor.do";
	}
}
